package com.xqiapp.bean;

import com.xqiapp.utils.ValidUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 
 * 数据返回封装类
 * 
 * @author demo7
 *
 */
public class Resultx extends CompareModel {

	private static final long serialVersionUID = 1L;

	/**
	 * 默认返回(失败)
	 */
	public static final String FAIL = "fail";
	/**
	 * 成功返回
	 */
	public static final String SUCCESS = "success";

	/**
	 * 返回标识符 ,默认为错误（false）的返回
	 */
	private boolean status = true;
	/**
	 * 返回的消息 默认为（fail）
	 */
	private String message = FAIL;

	/**
	 * 返回数据
	 */
	private Map<String, Object> data;

	public Resultx() {
	}

	/**
	 * 单独设置status
	 *
	 * @param status
	 */
	public Resultx(boolean status) {
		if (status) {
			this.message = SUCCESS;
		}
		this.status = status;
	}

	public Resultx(String message) {
		this.message = message;
	}

	public Resultx(boolean status, String message) {
		this.status = status;
		this.message = message;
	}

	public Resultx message(boolean status, String message) {
		this.status = status;
		this.message = message;
		return this;
	}

	public Resultx data(boolean status, Map<String, Object> data) {
		if (status) {
			this.message = SUCCESS;
		}
		this.status = status;
		this.data = data;
		return this;
	}

	public boolean isStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	public String getMessage() {
		return this.message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Map<String, Object> getData() {
		return data;
	}

	public void setData(Map<String, Object> data) {
		this.data = data;
	}

	/**
	 * 向data中放入值
	 * 
	 * @param key
	 * @param value
	 * @return Object
	 */
	public Object put(String key, Object value) {
		if (ValidUtil.isEmpty(data)) {
			data = new HashMap<String, Object>();
		}
		return data.put(key, value);
	}

	/**
	 * 向data中放入值
	 *
	 * @param key
	 * @param value
	 * @return this
	 */
	public Resultx add(String key, Object value) {
		if (ValidUtil.isEmpty(data)) {
			data = new HashMap<String, Object>();
		}
		return this;
	}

	/**
	 * 返回data 中的值
	 * 
	 * @param key
	 * @return Object
	 */
	public Object get(String key) {
		if (ValidUtil.isEmpty(data))
			return null;
		return data.get(key);
	}

	public Long getLong(String key) {
		if (ValidUtil.isEmpty(get(key)))
			return null;
		return Long.valueOf(get(key).toString());
	}

	public Integer getInt(String key) {
		if (ValidUtil.isEmpty(get(key)))
			return null;
		return Integer.parseInt(get(key).toString());
	}

	public String getString(String key) {
		if (ValidUtil.isEmpty(get(key)))
			return null;
		return get(key).toString();
	}

	public Double getDouble(String key) {
		if (ValidUtil.isEmpty(get(key)))
			return null;
		return Double.valueOf(get(key).toString());
	}

	public Boolean getBoolean(String key) {
		return Boolean.parseBoolean(getString(key));
	}

	/**
	 * 判断data中是否有相同的key
	 * 
	 * @param key
	 * @return boolean
	 */
	public boolean containsKey(String key) {
		if (ValidUtil.isEmpty(data))
			return false;
		return data.containsKey(key);
	}

	public Resultx status(boolean status) {
		this.status = status;
		return this;
	}
}
